﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace Rally_like_a_madman.Data
{
    [Table]
    public class Score : INotifyPropertyChanged, INotifyPropertyChanging
    {
        public event PropertyChangingEventHandler PropertyChanging;
        public event PropertyChangedEventHandler PropertyChanged;

        private int idScoreValue;
        private Nullable<int> idRoute;
        private Nullable<int> idUser;
        private int timeValue;
        private EntityRef<Route> routeValue;
        private EntityRef<User> userValue;

        [Column (IsPrimaryKey = true, IsDbGenerated = true)]
        public int IDScore
        { 
            get 
            { 
                return idScoreValue; 
            } 
            private set 
            {
                NotifyPropertyChanging("IDScore");
                idScoreValue = value;
                NotifyPropertyChanged("IDScore"); 
            }
        }

        [Column(Storage = "idRoute", DbType = "Int")]
        public int? IDRoute
        {
            get
            {
                return this.idRoute;
            }
            set
            {
                NotifyPropertyChanging("IDRoute");
                this.idRoute = value;
                NotifyPropertyChanged("IDRoute");
            }
        }

        [Column(Storage = "idUser", DbType = "Int")]
        public int? IDUser
        {
            get
            {
                return this.idUser;
            }
            set
            {
                NotifyPropertyChanging("IDUser");
                this.idUser = value;
                NotifyPropertyChanged("IDUser");
            }
        }

        [Column]
        public int Time
        {
            get
            {
                return timeValue;
            }
            set
            {
                NotifyPropertyChanging("Time");
                timeValue = value;
                NotifyPropertyChanged("Time");
            }
        }

        [Association(IsForeignKey = true, Storage = "routeValue", ThisKey = "IDRoute")]
        public Route Route
        {
            get
            {
                return routeValue.Entity;
            }
            set
            {
                NotifyPropertyChanging("Route");
                routeValue.Entity = value;
                NotifyPropertyChanged("Route");
            }
        }

        [Association(IsForeignKey = true, Storage = "userValue", ThisKey = "IDUser")]
        public User User
        {
            get
            {
                return userValue.Entity;
            }
            set
            {
                NotifyPropertyChanging("User");
                userValue.Entity = value;
                NotifyPropertyChanged("User");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="propertyName"></param>
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="propertyName"></param>
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return User.Name + " - " + TimeSpan.FromSeconds(Time);
        }
    }
}
